

package com.yeyks.modules.sys.controller;

import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.account.dal.domain.SysDept;
import com.yeyks.account.service.SysDeptService;
import com.yeyks.annotation.Login;
import com.yeyks.annotation.LoginUser;
import com.yeyks.common.utils.Constant;
import com.yeyks.common.utils.Result;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;


/**
 * 部门管理
 *
 * @author administrator
 */
@Api(tags = "部门管理")
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController extends AbstractController {
	@Autowired
	private SysDeptService sysDeptService;
	
	/**
	 * 列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@RequiresPermissions("sys:dept:list")
	public List<SysDept> list(){
		List<SysDept> deptList = sysDeptService.queryList(new HashMap<String, Object>());

		return deptList;
	}

	/**
	 * 选择部门(添加、修改菜单)
	 */
	@RequestMapping(value = "/select", method = RequestMethod.POST)
	@RequiresPermissions("sys:dept:select")
	public Result select(){
		List<SysDept> deptList = sysDeptService.queryList(new HashMap<String, Object>());

		//添加一级部门
//		if(getUserId() == Constant.SUPER_ADMIN){
//			SysDept root = new SysDept();
//			root.setDeptId(0L);
//			root.setName("一级部门");
//			root.setParentId(-1L);
//			root.setOpen(true);
//			deptList.add(root);
//		}

		return Result.ok().put("deptList", deptList);
	}

	/**
	 * 上级部门Id(管理员则为0)
	 */
	@Login
	@RequestMapping(value = "/info", method = RequestMethod.POST)
	@RequiresPermissions("sys:dept:list")
	public Result info(@ApiIgnore @LoginUser AccountInfo loginUser){
		long deptId = 0;
		if(loginUser.getId() != Constant.SUPER_ADMIN){
			List<SysDept> deptList = sysDeptService.queryList(new HashMap<String, Object>());
			Long parentId = null;
			for(SysDept SysDept : deptList){
				if(parentId == null){
					parentId = SysDept.getParentId();
					continue;
				}

				if(parentId > SysDept.getParentId().longValue()){
					parentId = SysDept.getParentId();
				}
			}
			deptId = parentId;
		}

		return Result.ok().put("deptId", deptId);
	}
	
	/**
	 * 信息
	 */
	@RequestMapping(value = "/info/{deptId}", method = RequestMethod.POST)
	@RequiresPermissions("sys:dept:info")
	public Result info(@PathVariable("deptId") Long deptId){
		SysDept dept = sysDeptService.getById(deptId);
		
		return Result.ok().put("dept", dept);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@RequiresPermissions("sys:dept:save")
	public Result save(@RequestBody SysDept dept){
		sysDeptService.save(dept);
		
		return Result.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@RequiresPermissions("sys:dept:update")
	public Result update(@RequestBody SysDept dept){
		sysDeptService.updateById(dept);
		
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@RequiresPermissions("sys:dept:delete")
	public Result delete(long deptId){
		//判断是否有子部门
		List<Long> deptList = sysDeptService.queryDetpIdList(deptId);
		if(deptList.size() > 0){
			return Result.error("请先删除子部门");
		}

		sysDeptService.removeById(deptId);
		
		return Result.ok();
	}
	
}
